<?php
/*
 *管理员管理
 */
class Admin extends CI_Controller
{
	function __construct(){
		parent::__construct();
		$admin_id = $this->session->userdata('admin_id');
        if ( empty($admin_id ) ) {
			$this->load->helper('url');
        	redirect('login/index','refresh');
        }
        power($admin_id);
    }
    /*
     * 管理员列表
     */
	function index()
	{
        $where = array();
		$total_rows = $this->db->like($where)->from('admin')->count_all_results();
		$per_page = 10;
		$this->load->library('page',array('total'=>$total_rows,'perpage'=>$per_page));
		$data['page'] = $this->page->show(1);
		$this->load->model('admin_model','mAdmin',TRUE);
        $data['results'] = $this->mAdmin->get_admin_list($per_page,$this->page->offset(),$where);
        $this->load->view('admin/index', $data);
	}
	/*
	 * 管理员添加
	 */
	function add()
	{
		if ( !is_post() ){
			$this->load->view('admin/add');
		}else{
			if ($_POST['password']!=$_POST['repassword']){
				display('密码不一致',site_url("admin/add"));
			}
			$msg = array();
			$msg['account'] = $_POST['account'];
			$msg['password'] = md5($_POST['password']);
			$msg['type'] = $_POST['type'];
			$msg['addtime'] = time();
			$this->db->insert('admin', $msg);
			$last_id = $this->db->insert_id();
			if ( $last_id>0 ){
				display('添加成功',site_url("admin/add"));
			}else{
				display('添加失败',site_url("admin/add"),0);
			}
		}
	}
	/*
	 * 修改密码
	 */
	function edit($id)
	{
		if ( !is_post() ){
			$query = $this->db->from("admin")->where('id', $id)->get();
			$data['row'] = $query->row();
			$this->load->view('admin/edit', $data);
		}else{
			if ($_POST['password']!=$_POST['repassword']){
				display('密码不一致',site_url("admin/edit/{$id}"));
			}
			$msg = array();
			$msg['password'] = md5($_POST['password']);
			$msg['type'] = $_POST['type'];
			$this->db->where('id',$id);
			$this->db->update('admin', $msg);
			$affected_rows = $this->db->affected_rows();
			if ( $affected_rows>0 ){
				display('修改成功',site_url("admin/edit/{$id}"));
			}else{
				display('修改失败',site_url("admin/edit/{$id}"),0);
			}
		}
	}
	/*
	 * 删除管理员
	 */
	function delete($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('admin');
		display('删除成功',site_url("admin/index"));
	}
	/*
	 * 冻结玩家
	 */
	function freezing()
	{
		if ( !is_post() ){
			$this->load->view('admin/freezing');
		}else{
			$addtime = time();
			$data = array();
			$role_id = get_table_info('role','id',array('name'=>$_POST['role_name']));
			if(empty($role_id)){
				display('角色名不存在',site_url("admin/freezing"));
				return false;
			}
			$data['role_id'] = $role_id;
			$freezing_day = $_POST['freezing_day'];
			$data['freezing_day'] = $freezing_day;
			$data['intro'] = $_POST['intro'];
			$data['addtime'] = $addtime;
			$admin_id = $this->session->userdata('admin_id');
			$data['admin_id'] = $admin_id;
			$this->db->insert('freezing_record', $data);
			$last_id = $this->db->insert_id();
			if ( $last_id>0 ){
				$msg['freezing_time'] = $addtime + $freezing_day*24*60*60;
				$this->db->where('id',get_table_info('role','user_id',array('name'=>$_POST['role_name'])));
				$this->db->update('user',$msg);
				$affected_rows = $this->db->affected_rows();
				if ( $affected_rows>0 ){
					display('冻结成功',site_url("admin/freezing"));
				}else{
					display('数据错误',site_url("admin/freezing"),0);
				}
			}else{
				display('冻结失败',site_url("admin/freezing"),0);
			}
		}
	}
}
?>